/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.supinfo.youfood.web.servlet.cuisine;

import com.supinfo.youfood.entity.*;
import com.supinfo.youfood.service.CommandeService;
import com.supinfo.youfood.service.MessageService;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import javax.ejb.EJB;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.TextMessage;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Michael
 */
@WebServlet(name = "AccueilCuisineServlet", urlPatterns = {"/cuisine"})
public class AccueilCuisineServlet extends HttpServlet {
    
    @EJB
    private MessageService messageService;
    
    @EJB
    private CommandeService commandeService;
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Enumeration e = messageService.getAllMessages();
        ArrayList queue = Collections.list(e);
        List<Message> m = queue;
        List<Commande> commandes = new ArrayList<Commande>();
        
        
        for(int i=0;i<m.size();i++) {
            try {
                long idCommande = Long.valueOf(((TextMessage)m.get(i)).getText());
                Commande tempCommande = commandeService.findCommandeById(idCommande);
                if(tempCommande != null) {
                    commandes.add(tempCommande);
                }
            } catch (JMSException ex) {
                System.out.println(ex.getMessage());
            }
        }
        request.setAttribute("commandes", commandes);
        request.getRequestDispatcher("/jsp/cuisine/cuisine.jsp").forward(request, response);
    }
    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
    }
}
